package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.init.PKInit;
import com.sunmnet.j2ee.core.pub.sqlloader.SqlLoader;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.dao.BaseSqlDao;
import com.sunmnet.j2ee.entity.powerdb.designer.AccountManage;
import com.sunmnet.j2ee.entity.security.Account;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.AccountManageService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈振忠
 * on 2016/7/28.
 */
@Service("/powerdb/designer/accountManageService")
public class AccountManageServiceImpl implements AccountManageService {

    @Resource(name = "baseSqlDao")
    private BaseSqlDao dao;

    /**
     * log4j2日志 *
     */
    private static final Logger LOG = LogManager.getLogger(AccountManageServiceImpl.class.getName());

    private static final String CLASS_NAME = AccountManageServiceImpl.class.getName();

    private static final String SQL_SELECT_ACCOUNT_REL_PROJECT = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_ACCOUNT_REL_PROJECT");

    private static final String SQL_DELETE_ACCOUNT_REL_PROJECT = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_DELETE_ACCOUNT_REL_PROJECT");

    private static final String SQL_INSERT_DB = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_INSERT_DB");

    private static final String SQL_DEL_ACCOUNTRELDB = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_DEL_ACCOUNTRELDB");

    private static final String SQL_SELECT_ALL_DB = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_ALL_DB");

    private static final String SQL_SELECT_ALL_PRO = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_ALL_PRO");

    private static final String SQL_SEARCH_BY_ALL = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SEARCH_BY_ALL");

    private static final String SQL_SELECT_ALL = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_ALL");


    @Override
    public short saveAccountRelProject(Long accountId, String projectIds, short right) {
        Map<String, Object> params = new HashMap<String, Object>();
        String[] proId = projectIds.split(",");
        int resule = -1;
        for (String pro : proId) {
            params.put("id", PKInit.pk.getID());
            params.put("accountId", accountId);
            params.put("projectId", Long.parseLong(pro));
            params.put("projectAuto", right);
            LOG.debug(SQL_SELECT_ACCOUNT_REL_PROJECT);
            resule = dao.executeSql(SQL_SELECT_ACCOUNT_REL_PROJECT, params);
        }
        return (short) resule;
    }


    @Override
    public short deleteAccountRleProject(Long accountId, String projectIds) {
        Map<String, Object> params = new HashMap<String, Object>();
        int resule = -1;
        params.put("accountId", accountId);
        //params.put("projectIds", projectIds);;
        String sql = SQL_DELETE_ACCOUNT_REL_PROJECT.replace(":projectIds", projectIds);
        resule = dao.executeSql(sql, params);

        return (short) resule;
    }

    @Override
    public short saveAccountRelDbConnInfo(Long accountId, String dbConnInfoIds) {
        int result = -1;
        Map<String, Object> params = new HashMap<String, Object>();
        String[] dbIds = dbConnInfoIds.split(",");
        for (String dbId : dbIds) {
            params = new HashMap<String, Object>();
            params.put("id", PKInit.pk.getID());
            params.put("accountId", accountId);
            params.put("dbId", Long.parseLong(dbId));
            LOG.debug("params: {}", params);
            result = dao.executeSql(SQL_INSERT_DB, params);
        }
        return (short) result;
    }

    @Override
    public short deleteAccountRelDbConnInfo(Long accountId, String dbConnInfoIds) {
        int result = -1;
        Map<String, Object> params = new HashMap<String, Object>();
        String[] dbIds = dbConnInfoIds.split(",");
        for (String dbId : dbIds) {
            params.put("accountId", accountId);
            params.put("dbId", Long.parseLong(dbId));
            result = dao.executeSql(SQL_DEL_ACCOUNTRELDB, params);
        }
        return (short) result;
    }

    @Override
    public List getAccountDb(long accountId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        List list = dao.findBySql(SQL_SELECT_ALL_DB,params);
        return list;
    }

    @Override
    public List getAccountProject(long accountId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        List list = dao.findBySql(SQL_SELECT_ALL_PRO, params);
        return list;
    }

     @Override
    public List<Map<String,Object>> findAllAccount() {

        return dao.findBySql(SQL_SELECT_ALL);
    }

    @Override
    public List<Map<String, Object>> findByAllSearch(QueryFilter filter) {
        filter.setSql(SQL_SEARCH_BY_ALL);
        String sql = filter.createSql();
        LOG.debug(sql);
        return dao.findBySql(sql);
    }


}
